package com.ct.goods.responsity.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ct.goods.mapper.GoodsSeckillMapper;
import com.ct.goods.orm.GoodsSeckill;
import com.ct.repository.commons.BaseServiceImpl;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 秒杀商品仓库
 *
 * @author system
 */
@Repository
public class AdminGoodsSeckillRepository extends BaseServiceImpl<GoodsSeckillMapper, GoodsSeckill> implements IAdminGoodsSeckillRepository {

    /**
     * 修改秒杀商品属性
     *
     * @param id            主键
     * @param originalPrice 原价
     * @param price         一口价(秒杀价)
     * @param stock         库存
     * @param startTime     秒杀开始时间
     * @param endTime       秒杀结束时间
     */
    @Override
    public boolean update(Long id, BigDecimal originalPrice, BigDecimal price,
                          Integer stock, Date startTime, Date endTime) {
        this.lambdaUpdate()
                .set(originalPrice != null, GoodsSeckill::getOriginalPrice, originalPrice)
                .set(price != null, GoodsSeckill::getPrice, price)
                .set(stock != null, GoodsSeckill::getStock, stock)
                .set(startTime != null, GoodsSeckill::getStartTime, startTime)
                .set(endTime != null, GoodsSeckill::getEndTime, endTime)
                .eq(GoodsSeckill::getId, id)
                .update();
        return false;
    }

    public GoodsSeckill getByGoodsId(Long goodsId) {
        if (goodsId == null) {
            return null;
        }
        LambdaQueryWrapper<GoodsSeckill> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsSeckill::getGoodsId,goodsId);
        return this.getOne(lambdaQueryWrapper);
    }

}
